/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

/**
 *
 * @author Zhang Ying
 */
@Entity
public class ArchivedIndivSOP implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Timestamp updatedTime;
    private boolean bySystem;
    
    private int archiveIndex;
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> salesforcastValues = new ArrayList<IndexNValue>(); // size of 3
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> unplannedDemandList = new ArrayList<IndexNValue>();  //length of the list should be 3
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> productionPlanList = new ArrayList<IndexNValue>();
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> inventoryList = new ArrayList<IndexNValue>();

    public ArchivedIndivSOP() {
    }

    public ArchivedIndivSOP(int archiveIndex, Timestamp currentTime, List<IndexNValue> salesforecastValues, List<IndexNValue> unplannedDemandList, List<IndexNValue> productionPlanList, List<IndexNValue> inventoryList) {
        this.archiveIndex = archiveIndex;
        bySystem = false;
        updatedTime = currentTime;
        for (int i = 0; i < 3; i++) {
            this.salesforcastValues.add(new IndexNValue(i, salesforecastValues.get(i).getLongValue()));
            this.unplannedDemandList.add(new IndexNValue(i, unplannedDemandList.get(i).getLongValue()));
            this.productionPlanList.add(new IndexNValue(i, productionPlanList.get(i).getLongValue()));
            this.inventoryList.add(new IndexNValue(i, inventoryList.get(i).getLongValue()));
        }
    }

    public ArchivedIndivSOP(int archiveIndex,List<IndexNValue> salesforecastValues, List<IndexNValue> unplannedDemandList, List<IndexNValue> productionPlanList, List<IndexNValue> inventoryList) {
        this.archiveIndex = archiveIndex;
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        Timestamp currentTime = new Timestamp(date.getTime());
        bySystem = true;
        updatedTime = currentTime;
        for (int i = 0; i < 3; i++) {
            this.salesforcastValues.add(new IndexNValue(i, salesforecastValues.get(i).getLongValue()));
            this.unplannedDemandList.add(new IndexNValue(i, unplannedDemandList.get(i).getLongValue()));
            this.productionPlanList.add(new IndexNValue(i, productionPlanList.get(i).getLongValue()));
            this.inventoryList.add(new IndexNValue(i, inventoryList.get(i).getLongValue()));
        }
    }

    public String getFormattedDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(updatedTime);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean isBySystem() {
        return bySystem;
    }

    public void setBySystem(boolean bySystem) {
        this.bySystem = bySystem;
    }

    public int getArchiveIndex() {
        return archiveIndex;
    }

    public void setArchiveIndex(int archiveIndex) {
        this.archiveIndex = archiveIndex;
    }

    public List<IndexNValue> getInventoryList() {
        return inventoryList;
    }

    public void setInventoryList(List<IndexNValue> inventoryList) {
        this.inventoryList = inventoryList;
    }

    public List<IndexNValue> getProductionPlanList() {
        return productionPlanList;
    }

    public void setProductionPlanList(List<IndexNValue> productionPlanList) {
        this.productionPlanList = productionPlanList;
    }

    public List<IndexNValue> getSalesforcastValues() {
        return salesforcastValues;
    }

    public void setSalesforcastValues(List<IndexNValue> salesforcastValues) {
        this.salesforcastValues = salesforcastValues;
    }

    public List<IndexNValue> getUnplannedDemandList() {
        return unplannedDemandList;
    }

    public void setUnplannedDemandList(List<IndexNValue> unplannedDemandList) {
        this.unplannedDemandList = unplannedDemandList;
    }

    public Timestamp getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Timestamp updatedTime) {
        this.updatedTime = updatedTime;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof ArchivedIndivSOP)) {
            return false;
        }
        ArchivedIndivSOP other = (ArchivedIndivSOP) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.ppm.entity.ArchivedIndivSOP[ id=" + id + " ]";
    }
}
